
import { _decorator, Component, Node } from 'cc';
import { BaseTransform } from './BaseTransform';
import GameData from '../../Script/Common/GameData';
import { GlobalEnum } from '../../Script/GameSpecial/GlobalEnum';
import GameConfig from '../../Script/GameSpecial/GameConfig';
import Action3dManager, { Action3d, ActionMngType } from '../../Script/Common/Action3dManager';
import { Role } from './Role';
const { ccclass, property } = _decorator;

@ccclass('CarPart')
export class CarPart extends BaseTransform {

    protected initSub() {
        this.initAction();
        this.initCollScope();
        this.initDirtyFlags();
    }
    protected resetSub() {
        this.resetAction();
        this.resetCollScope();
        this.resetDirtyFlags();
    }
    protected setData(data: { z: number, r: number }) {
        this.node.setPosition(0, 0, data.z);
        let collData = GameData.getData(GlobalEnum.GameDataType.CollItemData, this.node.name);
        let radius = collData.radius;
        this.minZ = data.z - radius;
        this.maxZ = data.z + radius;
        let r = radius / GameConfig.GameRule.roadHalfWidth;
        this.minRadian = data.r - r;
        this.maxRadian = data.r + r;
        r = data.r * 0.5;
        this.node.setRotation(0, 0, Math.sin(r), Math.cos(r));
    }

    public checkColl(role: Role) {
        return role.minRadian <= this.maxRadian && role.maxRadian >= this.minRadian;
    }

    public onColl(role) {
        this.canColl = false;
        if (role.isPlayer) {
            this.node.active = false;
        } else if (this.node.active) {
            Action3dManager.getMng(ActionMngType.Level).runAction(this.modelNode, this.action);
        }
    }

    @property(Node)
    protected modelNode: Node = null;
    protected action: Action3d = null;
    protected originalScale: number = null;
    protected initAction() {
        this.originalScale = this.modelNode.scale.x;
        let s = 0;
        let scale0 = Action3dManager.scaleTo(0, s, s, s);
        let delay = Action3dManager.delay(0.3);
        let d = 0.3;
        s = this.originalScale;
        let scale1 = Action3dManager.scaleTo(d, s, s, s);
        let cb = Action3dManager.callFun(this.onScaleFinish, this);
        this.action = Action3dManager.sequence(scale0, delay, scale1, cb);
    }
    protected resetAction() {
        Action3dManager.getMng(ActionMngType.Level).stopAllActions(this.modelNode);
        let s = this.originalScale;
        this.modelNode.setScale(s, s, s);
        this.canColl = true;
    }
    protected onScaleFinish() {
        this.canColl = true;
    }
}
